package ex304.bank;

import java.util.Objects;

public class Account {
    protected String id;
    protected String name;
    protected double balance;

    // 2个构造方法
    public Account(String id, String name) {
        this(id, name, 0.0);
    }

    public Account(String id, String name, double balance) {
        this.id = id;
        this.name = name;
        this.balance = balance;
    }

    /**
     * 存款方法，增加余额
     *
     * @param amount 存款金额
     */
    public void deposit(double amount) {
        this.balance += amount;
    }

    /**
     * 取款方法，减少金额
     *
     * @param amount 取款金额
     * @return true 取款成功/ false 余额不中
     */
    public boolean withdraw(double amount) {
        if (amount > this.balance) {
            return false;
        } else {
            this.balance -= amount;
            return true;
        }
    }

    @Override
    public String toString() {
        return String.format("[%s, %s, %.2f]", id, name, balance);
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) return true;
        if (other == null || this.getClass() != other.getClass()) return false;
        Account account = (Account) other;
        return id.equals(account.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    // 只提供访问器方法
    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getBalance() {
        return balance;
    }
}
